<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>dicitem</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<meta name="generator" content="Geany 0.20" />
	<script type='text/javascript' src='ajax.js'></script>
</head>

<body>
<?php
/*
 *      diclist.php
 *      
 *      Copyright 2011 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/osmetadata.php");

function displayform(&$dictionary, &$item) {
	echo "item form";
	//echo "<pre>".print_r($item,1)."</pre>";
	$out = "<form name='edit' action='' method='post'>";
	$out .= "<table align='center' style='border: solid black 1px;'>";
	foreach($dictionary->fields->getall() as $field) {
		$out .= "<tr>";
		//$out .= "<th style='text-align:right;'>".$field->name."</th>";
		$out .= "<th style='text-align:right;'>".$field->name."</th>";
		//$out .= "<td style='border: solid black 1px;text-align:left;'><input type='text' name='".$field->uuid."' value='".$item->fields->byuuid($field->uuid)->value."'></td>";
		$out .= "<td style='border: solid black 1px;text-align:left;'>".$item->fields->byuuid($field->uuid)->form->get()."</td>";
		$out .= "</tr>";
	}
	$out .= "</table>";
	$out .= "<input type='submit' name='save' value='Сохранить'>";
	$out .= "</form>";
	echo $out;
}

function updatefields(&$dictionary, &$item) {
	foreach($dictionary->fields->getall() as $field) {
		$item->fields->byuuid($field->uuid)->value = $_POST[$field->uuid];
	}
	$item->save();
}

if (!array_key_exists("dictionary",$_GET)) {
	echo "Необходимо указать словарь";
	exit;
} else {
	if (!$dictionary = $metadata->dictionaries->byuuid($_GET["dictionary"])) {
		echo "Словарь с таким uuid не найден";
		exit;
	}
}

if (!array_key_exists("item",$_GET)) {
	echo "add item<br>";
	$item = $dictionary->items->add();
} else {
	if (!$item = $dictionary->items->byuuid($_GET["item"])) {
		echo "add item<br>";
		$item = $dictionary->items->add();
	}
}

//echo "<pre>".print_r($_POST,1)."</pre>";

if (array_key_exists("save",$_POST)) {
	echo "Сохранить";
	updatefields($dictionary, $item);
	displayform($dictionary, $item);
} elseif (array_key_exists("cancel",$_POST)) {
	
} else {
	displayform($dictionary, $item);
}


?>

	
</body>

</html>
